home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / loading / testload.c < prev    next >
C/C++ Source or Header  |  1989-04-03  |  4KB  |  169 lines

  1. /* begincopyright
  2.   Copyright (c) 1988 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Xerox PARC
  12.     3333 Coyote Hill Rd.
  13.     Palo Alto, CA
  14.   endcopyright */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/file.h>
  18. #include <sys/types.h>
  19. #include <setjmp.h>
  20. #include <xr/unwind.h>
  21.  
  22. struct LDGLOBAL ldglobal_real, *ldglobal = &ldglobal_real;
  23.  
  24.  
  25. extern char **environ;
  26.  
  27. static char sep[] = " \t\n";
  28.  
  29. extern char *sprintf();
  30. char f_name[128];
  31.  
  32. extern char **XR_argv;
  33.  
  34. XR_StartMainXR()
  35. {
  36.   int ret;
  37.   FILE *dbxinitfile;
  38.   int dbxinitfiledes, pid;
  39.   char name[128];
  40.   char *current;
  41.   char cmd[1024];
  42.   char func_name[128];
  43.   Installer_Func load_file(), installval;
  44.   
  45.   LDGLOBAL_loader_print = 1;
  46.   init_dyload(XR_argv[0], 0, 0);
  47.   
  48.   /* drop a XrshDBX into the local world to start a debugger. */
  49.   get_dyload_filename(name, "");
  50.   pid = getpid();
  51.   dbxinitfiledes = open("XrshDBX", O_CREAT | O_WRONLY, 0777);
  52.   dbxinitfile = fdopen(dbxinitfiledes, "w");
  53.   fprintf(dbxinitfile, "#!/bin/csh -f\n");
  54.   fprintf(dbxinitfile, "cp %s %s.dbx\n", name, name);
  55.   fprintf(dbxinitfile, "transform_symtab %s.dbx\n", name);
  56.   fprintf(dbxinitfile, "exec dbx -I. %s.dbx %d\n", name, pid);
  57.   fclose(dbxinitfile);
  58.   printf("'XrshDBX' command installed in current directory.\n");
  59.   while (!feof(stdin)) {
  60.     printf("Enter command: ");
  61.     if (gets(cmd) == NULL)
  62.       break;
  63.     if (! isatty(fileno(stdin))) {
  64.       /* echo command if not reading from a tty */
  65.       printf("%s\n", cmd);
  66.     }
  67.     current = strtok(cmd, sep);
  68.     if (current == 0) current = "";
  69.     switch (current[0]) {
  70.     case '?': case '\0': {
  71.       printcmds();
  72.       break;
  73.     }
  74.     case 's': {
  75.       int secs;
  76.       if ((current = strtok(NULL, sep)) == NULL) {
  77.     printcmds();
  78.     break;
  79.       }
  80.       secs = atol(current);
  81.       printf("Sleeping %d seconds.\n", secs);
  82.       sleep(secs);
  83.       break;
  84.     }
  85.     case 'l': {
  86.       if ((current = strtok(NULL, sep)) == NULL) {
  87.     printcmds();
  88.     break;
  89.       }
  90.       strcpy(f_name, current);
  91.       time_load();
  92.       break;
  93.     }
  94.     case 'c': {
  95.       int (*func)(), (*complete_get_sym_val())(), (*get_sym_val())();
  96.       if ((current = strtok(NULL, sep)) == NULL) {
  97.     printcmds();
  98.     break;
  99.       }
  100.       if ((func = get_sym_val(current)) == NULL) {
  101.     printf("Couldn't find function '%s' to call.\n", current);
  102.       } else {
  103.     printf("'%s' returns %x\n", current, (*func)(current));
  104.       }
  105.       break;
  106.     }
  107.     case 'd': {
  108.       char name[128];
  109.       char num[128];
  110.       int pid;
  111.       get_dyload_filename(name, "");
  112.       pid = getpid();
  113.       if (! fork()) {
  114.     /* run debugger in a child */
  115.     int fd;
  116.     /* close weird fd's (kludge!) */
  117.     for (fd=3; fd < 20; fd++)
  118.       (void) close(fd);
  119.     sprintf(num, "%d", pid);
  120.     execle("/usr/bin/dbxtool", "dbxtool", name, num, 0, environ);
  121.     
  122.     /* Should not get here */
  123.     printf("Execve failed.\n");
  124.     abort();
  125.       }
  126.       break;
  127.     }
  128.     case 'q': {
  129.       goto leaveloop;
  130.       break;
  131.     }
  132.     } /* end of switch */
  133.   } /* end of while */
  134.  leaveloop:
  135.   {    
  136.     char name[128];
  137.     get_dyload_filename(name, "");
  138.     unlink(name);
  139.     strcat(name, ".dbx");
  140.     unlink(name);
  141.   }
  142.   printf("\nNormal termination.\n");
  143. }
  144.  
  145. dumphex(s, len)
  146.      char *s;
  147. {
  148.   for (; len > 0; len--) {
  149.     printf(" %2x", 0xff & *s);
  150.     s += 1;
  151.   }
  152.   printf("\n");
  153. }
  154.  
  155. printcmds()
  156. {
  157.   printf("Commands are:\nload file\ndebug\nsleep secs\ncall _funcname\nquit\nprint\n");
  158. }
  159.  
  160. noarg_load()
  161. {
  162.   load_file(f_name, NULL, 1, 0);
  163. }
  164.  
  165. time_load()
  166. {
  167.   time_func_call_with_children(noarg_load, 1);
  168. }
  169.